﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AdminManage.aspx.cs" Inherits="Basic_Background_AdminManage" %>
<script type="text/javascript">
window.addEvent('domready',function(){

window.showAdminAddForm=function(){
    MUI.openContentByWindow('windowAdminAdd','添加系统用户',$('formContainer'),$('formAdminAdd'),400,170);
};

window.showUpdateAdminForm=function(r){
    var data=adminListdatagrid.getDataByRow(r);
    var formAdminUpdate=$('formAdminUpdate');
    var ctrl;
    ctrl=formAdminUpdate.getElement('.adminId');
    ctrl.set('value',data.adminId.toString());
    ctrl=formAdminUpdate.getElement('.adminName');
    ctrl.set('text',data.adminName);
    Util.setSelectedValue('updateAdminGroupId',data.adminGroup.groupId.toString());
    MUI.openContentByWindow('windowAdminUpdate','修改系统用户',$('formContainer'),$('formAdminUpdate'),400,170);
};

window.deleteAdmin=function(){
    var indices = window.adminListdatagrid.getSelectedIndices();
	if (indices.length == 0)
	{
		MUI.showModalAlert('您必须至少选定一行');
		return;
	}
	MUI.showConfirm('您确定要删除所选定的行？',function(){
	    var adminIds='';
	    for (var i=0; i<indices.length; i++)
	    {
		    adminIds+=window.adminListdatagrid.getDataByRow(indices[i]).adminId+',';
	    }
	    MUI.showModalMessage('正在删除系统用户');
	    new Request.JSON({url:'AdminManage.aspx?Action=deleteAdmin&adminIds='+adminIds,noCache:true,onSuccess:function(result){
	        MUI.hideModalMessage();
            if(result.error)
                MUI.appendStatusMessage({'text':result.msg});
            else
            {
                MUI.appendStatusMessage({'text':result.msg});
                window.adminListdatagrid.loadData();
            }
        },onFailure:function(error){
	        MUI.appendStatusMessage({'text':'AJAX提交表单的过程中发生未知错误。'});
	        MUI.hideModalMessage();
        }}).get();
	});
};

window.adminAddFormCheck=function(){
    new FormCheck('formAdminAdd',{display:{tipsPosition:'left'},submit:false,onValidateSuccess:function(){
        MUI.showModalMessage('正在添加系统用户');
        new Request.JSON({url:'AdminManage.aspx?Action=saveAddAdmin',noCache:true,onSuccess:function(result){
            MUI.hideModalMessage();
            if(result.error)
                MUI.appendStatusMessage({'text':result.msg});
            else
            {
                $('windowAdminAdd').retrieve('instance').close();
                MUI.appendStatusMessage({'text':result.msg});
                window.adminListdatagrid.loadData();
            }
	    },onFailure:function(error){
		    MUI.appendStatusMessage({'text':'AJAX提交表单的过程中发生未知错误。'});
		    MUI.hideModalMessage();
        }}).post($('formAdminAdd'));
    }});   
};

window.adminUpdateFormCheck=function(){
    new FormCheck('formAdminUpdate',{display:{tipsPosition:'left'},submit:false,onValidateSuccess:function(){
        MUI.showModalMessage('正在修改系统用户');
        new Request.JSON({url:'AdminManage.aspx?Action=updateAdmin',noCache:true,onSuccess:function(result){
            MUI.hideModalMessage();
            if(result.error)
                MUI.appendStatusMessage({'text':result.msg});
            else
            {
                $('windowAdminUpdate').retrieve('instance').close();
                MUI.appendStatusMessage({'text':result.msg});
                window.adminListdatagrid.loadData();
            }
	    },onFailure:function(error){
		    MUI.appendStatusMessage({'text':'AJAX提交表单的过程中发生未知错误。'});
		    MUI.hideModalMessage();
        }}).post($('formAdminUpdate'));
    }});   
};

window.loadAdminGroups=function(){
    new Request.JSON({url:'AdminGroupManage.aspx?Action=search&isadmin=0&page=-1&perpage=1',noCache:true,onSuccess:function(result){
        if(result.error)
            MUI.appendStatusMessage({'text':result.msg});
        else
        {
            for(var i=0;i<result.data.length;i++)
            {
                Util.addDistinctOption('adminGroupId',result.data[i].groupId.toString(),result.data[i].groupName);
                Util.addDistinctOption('searchAdminGroupId',result.data[i].groupId.toString(),result.data[i].groupName);
                Util.addDistinctOption('updateAdminGroupId',result.data[i].groupId.toString(),result.data[i].groupName);
            }
        }
    },onFailure:function(error){
	    MUI.appendStatusMessage({'text':'AJAX提交表单的过程中发生未知错误。'});
    }}).get();
};

window.binddingEventAdminManage=function(){
    $('searchAdminForm').getElement('.button').addEvent('click',function(){
        window.adminListdatagrid.options.page=1;
        window.adminListdatagrid.loadData();
    });
};

window.initializeAdminManage=function(){
    new ZebraTable({elements:'table.tableFixed'});
    window.adminListdatagrid = new omniGrid('adminList', {
        columnModel: [
        {header: 'ID',dataIndex: 'adminId',dataType:'number',width:80,sortable:true},
        {header: '用户名',dataIndex: 'adminName',dataType:'string',width:100,sortable:true},
        {header: '最后登录时间',dataIndex: 'lastLoginTime',dataType:'date',width:150,sortable:true},
        {header: '最后登录IP',dataIndex: 'lastLoginIp',dataType:'string',width:120},
        {header: '登录次数',dataIndex: 'loginTimes',dataType:'number',width:70,sortable:true},
        {header: '系统用户组',dataIndex: 'adminGroup',dataType:'string',width:120,labelFunction:function(rowdata, r, columnModel){
            return rowdata['adminGroup'].groupName;
        }},
        {header: '操作',dataIndex: 'adminId',dataType:'number',width:70,labelFunction:function(rowdata, r, columnModel){
            if(rowdata.adminName=='admin')
                return '&nbsp;';
            return '<a href="#" onclick="window.showUpdateAdminForm('+r.toString()+');">修改</a>';
        }}
        ],
        buttons : [
            {name: '添加', bclass: 'add', onclick : window.showAdminAddForm},
            {name: '删除所选', bclass: 'delete', onclick : window.deleteAdmin}
        ],
        url:"AdminManage.aspx?Action=search",
        urlMethod:'post',
        urlPostForm:'searchAdminForm',
        perPageOptions: [20,50,100,200,500,1000],
        perPage:20,
        height:$('workspace').getStyle('height').toInt()-17,
        serverSort:false,
        sortHeader:true,
        pagination:true
    });
    window.loadAdminGroups();
    window.binddingEventAdminManage();
    window.adminAddFormCheck();
    window.adminUpdateFormCheck();
};

window.initializeAdminManage();
});
</script>
<div id="adminList"></div>
<div id="formContainer" style="display:none;">
<form id="searchAdminForm">
    <span class="span">用户名</span>
    <input class="input" style="width:70px;" type="text" name="adminName" />
    <span class="span">用户组</span>
    <select id="searchAdminGroupId" class="select" name="adminGroupId">
        <option value="-1">所有用户组</option>
    </select>
    <input class="button" type="button" value="查询" />
</form>
<form id="formAdminAdd">
<table class="tableFixed" border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr>
        <td class="tableLeft1">用户名：</td>
        <td class="tableLeft2"><input class="input validate['required','alphanum','length[4,16]']" name="adminName" type="text" /></td>
    </tr>
    <tr>
        <td class="tableLeft1">密码：</td>
        <td class="tableLeft2"><input class="input validate['required','length[6,16]']" name="adminPass" type="password" /></td>
    </tr>
    <tr>
        <td class="tableLeft1">确认密码：</td>
        <td class="tableLeft2"><input class="input validate['required','length[6,16]']" name="confirmPassword" type="password" /></td>
    </tr>
    <tr>
        <td class="tableLeft1">请选择用户组：</td>
        <td class="tableLeft2"><select id="adminGroupId" class="input" name="adminGroupId"></select></td>
    </tr>
    <tr>
        <td class="tableLeft1"></td>
        <td class="tableLeft2"><input class="submit button validate['submit']" type="button" value="提交添加" /></td>
    </tr>
</table>
</form>
<form id="formAdminUpdate">
<input type="hidden" class="adminId" name="adminId" />
<table class="tableFixed" border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr>
        <td class="tableLeft1">用户名：</td>
        <td class="tableLeft2"><span class="adminName"></span></td>
    </tr>
    <tr>
        <td class="tableLeft1">密码：</td>
        <td class="tableLeft2"><input class="input validate['length[6,16]']" name="adminPass" type="password" />(不修改请留空)</td>
    </tr>
    <tr>
        <td class="tableLeft1">确认密码：</td>
        <td class="tableLeft2"><input class="input validate['length[6,16]']" name="confirmPassword" type="password" />(不修改请留空)</td>
    </tr>
    <tr>
        <td class="tableLeft1">请选择用户组：</td>
        <td class="tableLeft2"><select id="updateAdminGroupId" class="input" name="adminGroupId"></select></td>
    </tr>
    <tr>
        <td class="tableLeft1"></td>
        <td class="tableLeft2"><input class="submit button validate['submit']" type="button" value="提交修改" /></td>
    </tr>
</table>
</form>
</div>